package com.party.mobile.biz.crowdfund;

import com.alibaba.druid.support.json.JSONUtils;
import com.google.common.collect.Maps;
import com.party.core.exception.BusinessException;
import com.party.mobile.biz.currentUser.CurrentUserBizService;
import com.party.mobile.redisPubSub.publish.RedisMsgPublish;
import com.party.mobile.web.dto.AjaxResult;
import com.party.mobile.web.dto.login.output.CurrentUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName ProjectTransferNewBizService
 * @Description 支持者转移
 * @Author yifeng
 * @Date 2019/6/26 15:46
 **/
@Service
public class ProjectTransferNewBizService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProjectTransferBizService projectTransferBizService;

    @Autowired
    private CurrentUserBizService currentUserBizService;

    @Autowired
    private RedisMsgPublish redisMsgPublish;

    /**
     * @param sourceId 原众筹id
     * @param targetId 目标项目
     * @param request  request
     * @return com.party.mobile.web.dto.AjaxResult
     * @author yifeng
     * @description 众筹支持者转移
     * @date 14:27 2019/6/27
     **/
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AjaxResult transferSupport(String sourceId, String targetId, HttpServletRequest request) {
        Map<String, String> json = Maps.newHashMap();
        CurrentUser currentUser = currentUserBizService.getCurrentUser(request);
        String memberId = currentUser.getId();
        try {
            projectTransferBizService.transferSupport(sourceId, targetId, currentUser);
            return AjaxResult.success();
        } catch (BusinessException e) {
            json.put("result", "");
            json.put("message", e.getMessage());
            Map<String, String> newJson = Maps.newHashMap();
            json.put("memberId", memberId);
            newJson.putAll(json);
            String param = JSONUtils.toJSONString(newJson);
            redisMsgPublish.publish("projectTansferMobile", param);

            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error(e.getCode(), e.getMessage());
        } catch (Exception e) {
            json.put("result", "");
            json.put("message", "转移异常");
            Map<String, String> newJson = Maps.newHashMap();
            json.put("memberId", memberId);
            newJson.putAll(json);
            String param = JSONUtils.toJSONString(newJson);
            redisMsgPublish.publish("projectTansferMobile", param);

            logger.error("转移异常", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error(100, "转移失败");
        }
    }
}
